package demo2;

public class MyLinkedList {

    class ListNode{
        private int val;
        private ListNode next;

        public ListNode(int val){
            this.val = val;
        }
    }
    public ListNode head;

        //头插法
        public void addFirst(int data){
        ListNode newNode = new ListNode(data);
        newNode.next = head;
        head = newNode;
    }


        //尾插法
        public void addLast(int data){
            ListNode newNode = new ListNode(data);
            ListNode cur = head;
            if(head == null){
                head = newNode;
                return;
            }
            while(cur.next != null){
                cur = cur.next;
            }
            cur.next = newNode;
        }


        //任意位置插入,第一个数据节点为0号下标
        public boolean addIndex(int index,int data){
            if(index < 0 || index > size()){
                return false;
            }

            if(index == 0){
                addFirst(data);
                return true;
            }
            ListNode newNode = new ListNode(data);
            ListNode cur = head;
            for (int i = 0; i < index-1; i++) {
                cur = cur.next;
            }
            newNode.next = cur.next;
            cur.next = newNode;
            return true;
        }


        //查找是否包含关键字key是否在单链表当中
        public boolean contains(int key){
            ListNode cur = head;
            while(cur != null){
                if(cur.val == key){
                    return true;
                }
                cur = cur.next;
            }
            return false;
        }


        //删除第一次出现关键字为key的节点
        public void remove(int key){
            if (head == null) {
                return;
            }
            if (head.val == key) {
                head = head.next;
                return;
            }
            ListNode cur = head;
            while (cur.next != null) {
                if (cur.next.val == key) {
                    cur.next = cur.next.next;
                    return;
                }
                cur = cur.next;
            }
        }


        //删除所有值为key的节点
        public void removeAllKey(int key){
            if (head == null) {
                return;
            }
            // 处理头节点连续为 key 的情况
            while (head != null && head.val == key) {
                head = head.next;
            }
            ListNode cur = head;
            while (cur != null && cur.next != null) {
                if (cur.next.val == key) {
                    cur.next = cur.next.next; // 删除符合条件的节点
                } else {
                    cur = cur.next; // 继续遍历
                }
            }
        }


        //得到单链表的长度
        public int size(){
            ListNode cur = head;
            int length = 0;
            while(cur != null){
                length++;
                cur = cur.next;

            }
            return length;
        }


        public void display(){
            ListNode cur = head;
            while(cur != null){
                System.out.println(cur.val);
                cur = cur.next;
            }
            System.out.println();
        }


        public void clear(){
            head = null;
        }



}
